package com.maple.exclusivesport.adapters;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.maple.exclusivesport.ConfirmDialog;
import com.maple.exclusivesport.ContactInfoBean;
import com.maple.exclusivesport.GlideLoadUtils;
import com.maple.exclusivesport.R;
import com.maple.exclusivesport.SendEvent;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by maple on 2019/10/18 14:53
 * E-Mail Address：740917401@qq.com
 */
public class InnerAdapter extends BaseAdapter {
    private int cardWidth;
    private int cardHeight;
    Context context;
    List<ContactInfo> contactInfoList; 
    public InnerAdapter(Context context,int cardHeight,int cardWidth, List<ContactInfo> contactInfoList) {
        this.context=context;
        this.cardHeight=cardHeight;
        this.cardWidth=cardWidth;
        this.contactInfoList=contactInfoList;
    }

    public void addAll(Collection<ContactInfo> collection) {
        if (isEmpty()) {
            contactInfoList.addAll(collection);
            notifyDataSetChanged();
        } else {
            contactInfoList.addAll(collection);
        }
    }

    public void clear() {
        contactInfoList.clear();
        notifyDataSetChanged();
    }

    public boolean isEmpty() {
        return contactInfoList.isEmpty();
    }

    public void remove(int index) {
        if (index > -1 && index < contactInfoList.size()) {
            contactInfoList.remove(index);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getCount() {
        return contactInfoList.size();
    }

    @Override
    public ContactInfo getItem(int position) {
        if(contactInfoList==null ||contactInfoList.size()==0) return null;
        return contactInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    // TODO: getView
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ContactViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_new_item, parent, false);
            holder  = new ContactViewHolder(convertView);
            convertView.setTag(holder);
            convertView.getLayoutParams().width = cardWidth;
            holder.image = (ImageView) convertView.findViewById(R.id.image);
            holder.image.getLayoutParams().height = cardHeight;
            holder.content = (TextView) convertView.findViewById(R.id.content);
            holder.date = (TextView) convertView.findViewById(R.id.date);
            holder.weight = (TextView) convertView.findViewById(R.id.weight);
            //holder.delete = (ImageView) convertView.findViewById(R.id.delete);
        } else {
            holder = (ContactViewHolder) convertView.getTag();
        }


//contactInfoList中包含的都是ContactInfo类的对象
//通过其get()方法可以获得其中的对象
        ContactInfo ci = contactInfoList.get(position);

//将viewholder中hold住的各个view与数据源进行绑定(bind)
        holder.date.setText(ci.date);
        holder.weight.setText("Weight: "+ci.weight+" kg");
        holder.content.setText("Journal: "+ci.content);


        GlideLoadUtils.getInstance().glideLoad2(context, Uri.fromFile(new File(ci.image)),holder.image,-1);
//        holder.image.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                new PersonImageDialog(context,ci.image).show();
//            }
//        });
//        holder.delete.setOnClickListener((v)->
//                new ConfirmDialog(context, "提示", "请确认是否删除该条掉肉圈？", new ConfirmDialog.onConfirmDialogListener() {
//                    @Override
//                    public void onConfirm() {
//                        contactInfoList.remove(ci);
//                        EventBus.getDefault().post(new SendEvent());
//                    }
//
//                    @Override
//                    public void onCancel() {
//
//                    }
//                }).show()
//        );



        return convertView;
    }
}
